﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;
using System.Text;

public partial class service : System.Web.UI.Page
{
    private PostFactory m_PostFactory;
    private IPostService m_PostService;
    private int m_PostId = 0;
    private int m_NodeId = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();
        m_PostId = QueryStringHelper.GetInteger("id", 0);
        m_NodeId = QueryStringHelper.GetInteger("node", 0);

        if (!IsPostBack)
        {
            NodeVO nodeVO = m_PostService.GetNodeById(m_NodeId);
            if (nodeVO != null && m_PostId == 0)
            {
                Dictionary<string, string> conditions = new Dictionary<string, string>();
                conditions.Add("NodeId", m_NodeId.ToString());
                conditions.Add("Flag", "1");
                conditions.Add("PageIndex", "0");
                conditions.Add("PageSize", "1");
                IList<PostVO> list = m_PostService.GetPostList(conditions);
                if (list != null && list.Count > 0)
                {
                    m_PostId = list[0].PostId;
                }
            }

            if (nodeVO != null)
            {
                ltlTitle2.Text = nodeVO.Name;
            }

            PostVO postVO = m_PostService.GetPostById(m_PostId);
            if (postVO != null)
            {
                ltlTitle.Text = postVO.Title;                
                ltlContent.Text = postVO.HtmlContent;
            }

            FillService();
        }
    }

    private void FillService()
    {
        Dictionary<string, string> conditions = new Dictionary<string, string>();
        conditions.Add("NodeId", m_NodeId.ToString());
        conditions.Add("Flag", "1");
        IList<PostVO> list = m_PostService.GetPostList(conditions);
        StringBuilder sb = new StringBuilder();

        if (list != null && list.Count > 0)
        {
            foreach (PostVO postVO in list)
            {
                sb.AppendFormat("<li class='Service-Slect-LI'><a href='service.aspx?node={0}&id={1}' class='whitebtn'>{2}</a></li>", m_NodeId, postVO.PostId, postVO.Title);
            }

            ltlService.Text = sb.ToString();
        }
    }
}